package com.rzjm.exercise.sys.system.menu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rzjm.exercise.common.base.controller.BaseController;
import com.rzjm.exercise.common.base.data.DataTableLayUI;
import com.rzjm.exercise.common.constant.Constant;
import com.rzjm.exercise.common.log.annotation.Log;
import com.rzjm.exercise.common.utils.R;
import com.rzjm.exercise.sys.core.dict.annotation.DictBody;
import com.rzjm.exercise.sys.core.dict.domain.DictDO;
import com.rzjm.exercise.sys.core.dict.service.DictService;
import com.rzjm.exercise.sys.system.menu.domain.MenuDO;
import com.rzjm.exercise.sys.system.menu.service.MenuService;
import com.rzjm.exercise.sys.vo.Tree;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author bootdo 1992lcg@163.com
 */
@RequestMapping("/sys/menu")
@Controller
public class MenuController extends BaseController {
	String prefix = "system/menu";
	@Resource
	MenuService menuService;
	@Resource
	private DictService dictService;

	@RequiresPermissions("sys:menu:menu")
	@GetMapping()
	String menu(Model model) {
		model.addAttribute("parentId",0);
		return prefix+"/menu";
	}

	@RequiresPermissions("sys:menu:menu")
	@DictBody
	@ResponseBody
	@GetMapping("/list")
	public DataTableLayUI<MenuDO> list(@RequestParam Map<String, Object> params) {
		QueryWrapper queryWrapper = new QueryWrapper();
		queryWrapper.orderByAsc("order_num");
		List<MenuDO> list = menuService.list(queryWrapper);
		DataTableLayUI<MenuDO> dt = new DataTableLayUI<>();
		dt.setData(list);
		dt.setCount(list.size());
		System.out.println("list.toString()" + list.toString());
		return dt;
	}

	@Log("添加菜单")
	@RequiresPermissions("sys:menu:add")
	@GetMapping("/add/{pId}")
	String add(Model model, @PathVariable("pId") Integer pId) {
		model.addAttribute("pId", pId);
		if (pId == 0) {
			model.addAttribute("pName", "根目录");
		} else {
			model.addAttribute("pName", menuService.getById(pId).getName());
		}
		return prefix + "/add";
	}

	@Log("添加菜单批量生成功能按钮")
	@RequiresPermissions("sys:menu:batchAdd")
	@GetMapping("/batchAdd/{pId}")
	String batchAdd(Model model, @PathVariable("pId") Integer pId) {
		model.addAttribute("pId", pId);
		MenuDO menu = menuService.getById(pId);
		if (pId == 0) {
			model.addAttribute("pName", "根目录");
		} else {
			model.addAttribute("pName", menu.getName());
		}
		if(menu!=null){
			if(menu.getMenuType() == 1){
				model.addAttribute("menu", menu);
			}else if(menu.getMenuType() > 1){
				MenuDO parent = menuService.getById(menu.getParentId());
				model.addAttribute("menu", parent);
			}else{
				model.addAttribute("menu", new MenuDO());
			}
		}
		List<DictDO> list = dictService.listByType("menu-btn-type");
		model.addAttribute("btns",list);

		return prefix + "/batchAdd";
	}

	@Log("编辑菜单")
	@RequiresPermissions("sys:menu:edit")
	@GetMapping("/edit/{id}")
	String edit(Model model, @PathVariable("id") Integer id) {
		MenuDO mdo = menuService.getById(id);
		Integer pId = mdo.getParentId();
		model.addAttribute("pId", pId);
		if (pId == 0) {
			model.addAttribute("pName", "根目录");
		} else {
			model.addAttribute("pName", menuService.getById(pId).getName());
		}
		model.addAttribute("menu", mdo);
		return prefix+"/edit";
	}

	@Log("保存菜单")
	@RequiresPermissions("sys:menu:add")
	@PostMapping("/save")
	@ResponseBody
	R save(MenuDO menu) {
		if (Constant.DEMO_ACCOUNT.equals(getUsername())) {
			return R.error(1, "演示系统不允许修改,完整体验请部署程序");
		}
		if (menuService.save(menu)) {
			return R.ok();
		} else {
			return R.error(1, "保存失败");
		}
	}


	@Log("保存菜单")
	@RequiresPermissions("sys:menu:batchAdd")
	@PostMapping("/batchSave")
	@ResponseBody
	R batchSave(MenuDO menu) {
		if (Constant.DEMO_ACCOUNT.equals(getUsername())) {
			return R.error(1, "演示系统不允许修改,完整体验请部署程序");
		}

		int parentId = menu.getParentId();
		MenuDO parent = menuService.getById(parentId);
		if(parent != null){
			String url = menu.getUrl();
			String perms = url.replaceAll("/",":");
			String[] urlArr = url.split("/");

			if(parent.getMenuType() == 0){//在第一层下面添加
				MenuDO menuFirst = new MenuDO();
				menuFirst.setParentId(parentId);
				menuFirst.setName(menu.getName());
				menuFirst.setPerms(perms + ":" + urlArr[1]);
				menuFirst.setUrl(url);
				menuFirst.setMenuType(1);
				menuFirst.setShowType(1);
				menuFirst.setOrderNum(menu.getOrderNum());
				menuService.save(menuFirst);

				String btns = menu.getBtns();
				if(btns != null && !btns.equals("")){
					String[] btnsArr = btns.split(",");
					int index = 0;
					for (String dict_id : btnsArr){
						MenuDO menuDO = new MenuDO();
						DictDO dictDO = dictService.getById(dict_id);
						if(dictDO!=null){
							menuDO.setName(dictDO.getName());
							menuDO.setUrl(url + "/" + dictDO.getValue());
							menuDO.setPerms(perms + ":" + dictDO.getValue());
						}
						menuDO.setShowType(1);
						menuDO.setParentId(menuFirst.getMenuId());
						menuDO.setMenuType(menuFirst.getMenuType() + 1);
						menuDO.setOrderNum(index);
						menuService.save(menuDO);
						index ++;
					}
				}

			}else{
				String btns = menu.getBtns();
				if(btns != null && !btns.equals("")) {
					String[] btnsArr = btns.split(",");
					int index = 0;
					for (String dict_id : btnsArr) {
						MenuDO menuDO = new MenuDO();
						DictDO dictDO = dictService.getById(dict_id);
						if (dictDO != null) {
							menuDO.setName(dictDO.getName());
							menuDO.setUrl(url + "/" + dictDO.getValue());
							menuDO.setPerms(perms + ":" + dictDO.getValue());
						}
						menuDO.setShowType(1);
						menuDO.setParentId(parent.getMenuId());
						menuDO.setMenuType(parent.getMenuType() + 1);
						menuDO.setOrderNum(index);
						menuService.save(menuDO);
						index++;
					}
				}
			}
		}
		return R.ok();
	}

	@Log("更新菜单")
	@RequiresPermissions("sys:menu:edit")
	@PostMapping("/update")
	@ResponseBody
	R update(MenuDO menu) {
		if (Constant.DEMO_ACCOUNT.equals(getUsername())) {
			return R.error(1, "演示系统不允许修改,完整体验请部署程序");
		}
		if (menuService.updateById(menu)) {
			return R.ok();
		} else {
			return R.error(1, "更新失败");
		}
	}

	@Log("删除菜单")
	@RequiresPermissions("sys:menu:remove")
	@PostMapping("/remove")
	@ResponseBody
	R remove(Integer id) {
		if (Constant.DEMO_ACCOUNT.equals(getUsername())) {
			return R.error(1, "演示系统不允许修改,完整体验请部署程序");
		}
		if (menuService.removeById(id)) {
			return R.ok();
		} else {
			return R.error(1, "删除失败");
		}
	}

	@GetMapping("/tree")
	@ResponseBody
	Tree<MenuDO> tree() {
		Tree<MenuDO>  tree = menuService.getTree();
		return tree;
	}

	@GetMapping("/tree/{roleId}")
	@ResponseBody
	Tree<MenuDO> tree(@PathVariable("roleId") Integer roleId) {
		Tree<MenuDO> tree = menuService.getTree(roleId);
		return tree;
	}

	@GetMapping("/FontIcoList")
	String FontIcoList(Model model) {
		return "FontIcoList";
	}
}
